<?php

/**
 * This is the model class for table "baiviet".
 *
 * The followings are the available columns in table 'baiviet':
 * @property integer $IDbaiviet
 * @property string $tieudebaiviet
 * @property string $anhminhhoa
 * @property string $tomtat
 * @property string $noidung
 * @property string $IDloaitin
 * @property integer $trangthai
 * @property integer $thutu
 * @property string $ngaydang
 */
class Baiviet extends CActiveRecord
{
        const TT_HIEN = 0;
        const TT_AN = 1;

        public function getTrangthai()
        {
            return array(
            self::TT_HIEN=>'Hiển Thị',
                self::TT_AN=>'Ẩn',
            );
        }
        
        const TT1 = 1;
        const TT2 = 2;
        const TT3 = 3;
        const TT4 = 4;
        const TT5 = 5;
        const TT6 = 6;
        const TT7 = 7;
        const TT8 = 8;
        const TT9 = 9;
        const TT10 = 10;
        
        public function getThutu()
        {
            return array(
            self::TT1=>'1',
            self::TT2=>'2',
            self::TT3=>'3',
            self::TT4=>'4',
            self::TT5=>'5',
            self::TT6=>'6',
            self::TT7=>'7',
            self::TT8=>'8',
            self::TT9=>'9',
            self::TT10=>'10',
            );
        }
        
        

    /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'baiviet';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('trangthai', 'numerical', 'integerOnly'=>true),
                        array('anhminhhoa','file','types'=>'jpg,gif,png','allowEmpty'=>true),
                        //array('anhminhhoa','file','types'=>'jpg,gif,png'),
                        array('tieudebaiviet, tomtat, noidung, IDloaitin, trangthai, ngaydang, baivietdacbiet','required'),
			array('tieudebaiviet, anhminhhoa, tomtat, noidung, IDloaitin, ngaydang, baivietdacbiet', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('IDbaiviet, tieudebaiviet, anhminhhoa, tomtat, noidung, IDloaitin, trangthai, ngaydang, baivietdacbiet', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'IDbaiviet' => 'ID',
			'tieudebaiviet' => 'Tiêu đề',
			'anhminhhoa' => 'URL',
			'tomtat' => 'Tóm tắt',
			'noidung' => 'Nội dung',
			'IDloaitin' => 'Loại Tin',
			'trangthai' => 'Status',
			'ngaydang' => 'Ngày đăng',
                        'baivietdacbiet' => 'Bài viết đặc biệt'
		);
	}
        
        function SubstringNoidung()
        {
            if (strlen($this->noidung) <= 200  )
                return $this->noidung;
            else
                return substr($this->noidung, 0, 190) . '...';
        }
        
        public function SubstringTieude()
        {
            if (strlen($this->tieudebaiviet) <= 20  )
                return $this->tieudebaiviet;
            else
                return substr($this->tieudebaiviet, 0, 50) . '...';
        }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('IDbaiviet',$this->IDbaiviet);
		$criteria->compare('tieudebaiviet',$this->tieudebaiviet,true);
		$criteria->compare('anhminhhoa',$this->anhminhhoa,true);
		$criteria->compare('tomtat',$this->tomtat,true);
		$criteria->compare('noidung',$this->noidung,true);
		$criteria->compare('IDloaitin',$this->IDloaitin,true);
		$criteria->compare('trangthai',$this->trangthai);
		$criteria->compare('ngaydang',$this->ngaydang,true);
                $criteria->compare('baivietdacbiet', $this->baivietdacbiet,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function listByLoaiTin()
        {
                // Warning: Please modify the following code to remove attributes that
                // should not be searched.

                $criteria=new CDbCriteria;

                //$criteria->compare('username',$this->username,true);

                //$criteria->compare('email',$this->email,true);

                return new CActiveDataProvider(get_class($this), array(
                        'criteria'=>$criteria,
                        'sort'=>array(
                                'defaultOrder'=>'IDbaiviet ASC',
                        ),
                        'pagination'=>array(
                                'pageSize'=>5
                        ),
                ));
        }

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Baiviet the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
